package com.qzy.reggie.controller;

import com.qzy.reggie.common.PageResult;
import com.qzy.reggie.common.Result;
import com.qzy.reggie.pojo.Setmeal;
import com.qzy.reggie.service.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/setmeal")
public class MealController {
    @Autowired
    private MealService mealService;
    @GetMapping()
    public Result list(Integer page,Integer pageSize,String name){
        PageResult pageResult = mealService.queryList(page,pageSize,name);

        return Result.success("查询成功",pageResult);
    }

    @PostMapping()
    public Result save(@RequestBody Setmeal setmeal,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");
        // 完善系统信息
        setmeal.setCreateUser(empId);
        setmeal.setUpdateUser(empId);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());

        Integer ret = mealService.save(setmeal);
        if (ret > 0){
            return Result.success("新增成功");
        }else{
            return Result.error("新增失败");
        }
    }


    @GetMapping("{id}")
    public Result getSetmealById(@PathVariable Long id){
        return Result.success(mealService.queryById(id));
    }

    @PutMapping()
    public Result updateSetmeal(@RequestBody Setmeal setmeal,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(empId);

        Integer ret = mealService.update(setmeal);

        if (ret > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id){
        Integer ret = mealService.deleteById(id);

        if (ret > 0){
            return Result.success("修改成功");
        }else{
            return Result.error("修改失败");
        }
    }
}
